package com.gyt.manager.conf;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * description.
 *
 * @author gyt create on 2021/5/19 18:32
 */
@Configuration
@Slf4j
public class ExecutorConfig {

    @Bean("taskExecutor.label.collect")
    public ThreadPoolTaskExecutor taskExecutorLabelCollect() {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setCorePoolSize(10);
        taskExecutor.setMaxPoolSize(100);
        taskExecutor.setKeepAliveSeconds(20);
        taskExecutor.setQueueCapacity(50_0000);
        taskExecutor.setThreadNamePrefix("label-collect-thread-");
        taskExecutor.setRejectedExecutionHandler((r, e) -> {
            log.error("#taskPool is full and use callers to run![taskExecutor_common]" );
//            ContextHolder.getContext().getBean(IAlarmService.class)
//                    .sendAlarm("#taskPool monitor", "#taskPool is full and use callers to run![taskExecutor_uslineage_collect]");
            if (!e.isShutdown()) {
                r.run();
            }
        });
        taskExecutor.setAllowCoreThreadTimeOut(true);
        taskExecutor.initialize();
        return taskExecutor;

    }
}
